#include "QrCodeSource.h"
#include <opencv2/opencv.hpp>

CameraQrCodeSource::CameraQrCodeSource()
{
    pCap = new cv::VideoCapture(0);
    if (!pCap->isOpened())
    {
        std::cout << "open camera failed, please check camera" << std::endl;
		throw std::runtime_error("open camera failed, please check camera");
    }
}

CameraQrCodeSource::~CameraQrCodeSource()
{
    if (pCap != nullptr)
    {
        if (pCap->isOpened())
        {
            pCap->release();
        }

        delete pCap;
        pCap = nullptr;
    }
}

cv::Mat CameraQrCodeSource::getQrCode()
{
    cv::Mat image;
    *pCap >> image;
    return image;
}

CameraQrCodeSource& CameraQrCodeSource::operator>>(cv::Mat& image)
{
	cv::Mat innerImage = getQrCode();
    innerImage.copyTo(image);
    return *this;
}

AviQrCodeSource::AviQrCodeSource()
{
	std::string videoPath = "F:\\Download Files\\QrCodeTargetVideo.avi";
	pCap = new cv::VideoCapture(videoPath);
    if (!pCap->isOpened())
    {
		std::cout << "open Vide o failed, please check Vide: " << videoPath << std::endl;
		throw std::runtime_error("open Vide o failed, please check Vide: " + videoPath);
    }
}

AviQrCodeSource::~AviQrCodeSource()
{
    if (pCap != nullptr)
    {
        if (pCap->isOpened())
        {
            pCap->release();
        }

        delete pCap;
        pCap = nullptr;
    }
}

cv::Mat AviQrCodeSource::getQrCode()
{
    cv::Mat currentFrame;
	*pCap >> currentFrame;
	return currentFrame;
}

AviQrCodeSource& AviQrCodeSource::operator>>(cv::Mat& image)
{
    cv::Mat innerImage = this->getQrCode();
	innerImage.copyTo(image);
    return *this;
}
